package com.leran.redis.lock;

import com.alibaba.fastjson.JSONPath;
import com.leran.redis.lock.annotation.RedisLock;
import com.leran.redis.lock.annotation.RedisLockKey;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.CodeSignature;
import org.aspectj.lang.reflect.FieldSignature;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
@Slf4j
public class TestAop {

    @Pointcut("execution(* com.banniuyun.work.message.controller.sms..*(..))")
    public void servicePointcut() {
    }

    @Around("servicePointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        RedisLock lockAnnotation = method.getAnnotation(RedisLock.class);
        String typeName = joinPoint.getSignature().getDeclaringTypeName();
        String methodName = joinPoint.getSignature().getName();
        Object[] args = joinPoint.getArgs();
        //入参名称
        String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();
        FieldSignature fieldSignature = (FieldSignature) joinPoint.getSignature();
        RedisLockKey keyAnnotation = fieldSignature.getField().getAnnotation(RedisLockKey.class);
        String key = String.valueOf(JSONPath.read(JsonUtils.toJsonNoNull(args), "${" + keyAnnotation.name() + "}"));
        Object result = null;
        try {
            result = joinPoint.proceed(args);
            return result;
        } catch (Throwable throwable) {
            throw throwable;
        } finally {
            if (null != result) {
            } else {
            }
        }
    }
}
